package UCM;

import Source.LoginUser;
import Views.ViewLoginUser;

/**
 * The use case for login user
 * @author Jason and Josh
 */
public class UCLoginUser implements UCController {

    private LoginUser model = new LoginUser();
    private ViewLoginUser view;

    /**
     * Initualises the view
     * @param view
     */
    public UCLoginUser(ViewLoginUser view) {
        this.view = view;
    }

    /**
     * Precondition: User on Main UI.

    User enters their details and clicks login.
    System authenticates user.
    System logs user in.
    System adds user to list of online users.
    System updates UI indicating success.
    Postcondition: The main UI is updated.

    Unsuccessful Outcomes:

    2a) The authentication fails.
    System logs failed attempt.
    System refreshes UI indicating failure.
     */
    public void run() {
        view.loadTo(model);
        model.setUsername(view.getUserField());
        model.setPassword(view.getPasswordField());
        if (!model.authenticate()) {
            view.updateUIFailure();
        } else {
            model.addToOnline();
            view.updateUISuccess(model.getUsername());
        }

    }
}
